package mygang.controller;

import mygang.model.Error;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 */
@Controller
public class ErrorController extends AbstractController {

    @RequestMapping("/error")
    public ModelAndView error(@RequestParam String code) {
        ModelAndView model = new ModelAndView("error");
        Error error = new Error();
        
        //process error code
        try {
            error.setCode(Integer.parseInt(code));
            switch (error.getCode()) {
                case 403:   error.setMessage("Access denied");
                            break;
                default:    //TODO: find error code in DB
                            break;
            }
        } catch (NullPointerException npe){
            error.setCode(0);
            error.setMessage("Error code is not passed");
        } catch(NumberFormatException nfe){
            error.setCode(1);
            error.setMessage("Error code is not recognized");
        }
        
        model.addObject("error", error);
        return model;
    }
}
